package com.jandan.ui.client;

import java.util.List;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.SimpleStore;
import com.gwtext.client.data.Store;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.form.ComboBox;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.event.ComboBoxListenerAdapter;
import com.jandan.ui.client.service.WordLibService;
import com.jandan.ui.client.service.WordLibServiceAsync;
import com.jandan.ui.model.WordLib;

public class LibsFormPanel extends FormPanel {
	private ComboBox libNameComboBox = new ComboBox();
	private ComboBox wordsCountOfPerSmallLibComboBox = new ComboBox();

	public LibsFormPanel() {
		libNameComboBox.setForceSelection(true);
		libNameComboBox.setMinChars(1);
		libNameComboBox.setFieldLabel("词库");
		libNameComboBox.setDisplayField("wordLibName");
		libNameComboBox.setValueField("wordLibID");
		libNameComboBox.setMode(ComboBox.LOCAL);
		libNameComboBox.setTriggerAction(ComboBox.ALL);
		libNameComboBox.setTypeAhead(true);
		libNameComboBox.setSelectOnFocus(true);
		libNameComboBox.setEmptyText("请选择词库");
		libNameComboBox.setWidth(150);
		libNameComboBox.setEditable(false);
		libNameComboBox.setHideTrigger(false);
		libNameComboBox.setAllowBlank(false);
		libNameComboBox.setReadOnly(true);
		
		wordsCountOfPerSmallLibComboBox.setForceSelection(true);
		wordsCountOfPerSmallLibComboBox.setMinChars(1);
		wordsCountOfPerSmallLibComboBox.setFieldLabel("小词库单词数");
		wordsCountOfPerSmallLibComboBox.setDisplayField("wordsCountOfPerSmallLib");
		wordsCountOfPerSmallLibComboBox.setValueField("wordsCountOfPerSmallLib");
		wordsCountOfPerSmallLibComboBox.setMode(ComboBox.LOCAL);
		wordsCountOfPerSmallLibComboBox.setTriggerAction(ComboBox.ALL);
		wordsCountOfPerSmallLibComboBox.setTypeAhead(true);
		wordsCountOfPerSmallLibComboBox.setSelectOnFocus(true);
		wordsCountOfPerSmallLibComboBox.setEmptyText("请选择小词库单词数");
		wordsCountOfPerSmallLibComboBox.setWidth(150);
		wordsCountOfPerSmallLibComboBox.setLinked(true);
		wordsCountOfPerSmallLibComboBox.setEditable(false);
		wordsCountOfPerSmallLibComboBox.setHideTrigger(false);
		wordsCountOfPerSmallLibComboBox.setAllowBlank(false);
		wordsCountOfPerSmallLibComboBox.setReadOnly(true);
		
		this.add(libNameComboBox);
		this.add(wordsCountOfPerSmallLibComboBox);

		
		WordLibServiceAsync wordLibService = WordLibService.Util.getInstance();
		wordLibService.getWordLibList(new AsyncCallback<List<WordLib>>() {
			public void onFailure(Throwable caught) {
				MessageBox.alert(caught.getMessage());
			}

			public void onSuccess(List<WordLib> result) {
				// TODO Auto-generated method stub
				
//				MessageBox.alert("" + result.size());
				
				final Store libNameStore = new SimpleStore(new String[] { "wordLibID",
						"wordLibName" }, getWordLibName(result));
				libNameStore.load();
				libNameComboBox.setStore(libNameStore);
				
				final Store wordsCountOfPerSmallLibStore = new SimpleStore(new String[]{"wordLibID","wordsCountOfPerSmallLib"},getWordsCountOfPerSmallLib(result));
				wordsCountOfPerSmallLibStore.load();
				wordsCountOfPerSmallLibComboBox.setStore(wordsCountOfPerSmallLibStore);
				
				libNameComboBox.addListener(new ComboBoxListenerAdapter() {
		            public void onSelect(ComboBox comboBox, Record record, int index) {
		                wordsCountOfPerSmallLibComboBox.setValue("");
		                wordsCountOfPerSmallLibStore.filter("wordLibID", comboBox.getValue());
		            }
		        });
			}
		});
	}

//	private void setSotre(Store store) {
//		this.setStore(store);
//	}

	private Object[][] getWordLibName(List<WordLib> wordLibList) {
		Object[][] wordLibs = new Object[wordLibList.size()][];

		int size = wordLibList.size(); 
		for (int i = 0; i < size; i++) {
			wordLibs[i] = new Object[]{wordLibList.get(i).getWordLibID(),wordLibList.get(i).getWordLibName()};
//			wordLibs[i] = new String[2];
//			wordLibs[i][0] = "" + wordLibList.get(i).getWordLibID();
//			wordLibs[i][1] = wordLibList.get(i).getWordLibName();
		}
		return wordLibs;
	}
	
	private Object[][] getWordsCountOfPerSmallLib(List<WordLib> wordLibList){
		Object[][] wordsCountOfPerSmallLibs = new Object[wordLibList.size() * 4][];
		WordLib wordLib = null;
		for(int i = 0,n = wordLibList.size();i < n;i++){
			wordLib = wordLibList.get(i);
			wordsCountOfPerSmallLibs[i * 4] = new Object[]{wordLib.getWordLibID(),wordLib.getFirstClass()};
			wordsCountOfPerSmallLibs[i * 4 + 1] = new Object[]{wordLib.getWordLibID(),wordLib.getSecondClass()};
			wordsCountOfPerSmallLibs[i * 4 + 2] = new Object[]{wordLib.getWordLibID(),wordLib.getThirdClass()};
			wordsCountOfPerSmallLibs[i * 4 + 3] = new Object[]{wordLib.getWordLibID(),wordLib.getForthClass()};
		}
		
		return wordsCountOfPerSmallLibs;
	}
	
	public long getSelectedWordLibID(){
		return Long.parseLong(libNameComboBox.getValue());
	}

	public int getSelectedWordsCount() {
		// TODO Auto-generated method stub
		return Integer.parseInt(wordsCountOfPerSmallLibComboBox.getValue());
	}
}
